import sqlCore from "../mysql";
import Utils from "../utils";

class chatModel {
  async pensonalChatModel(form: any) {
    // 必选列
    const requiredColumns = [
      "user_id",
      "group_id",
      "content",
      "user_friends_id",
    ];

    // 所有列
    const allColumns = [...requiredColumns, "created_time"];

    // 获取有效值
    const validValues = await Utils.getValidValues(form, allColumns);

    // 创建一个包含列名和对应值的对象数组
    const columnValuePairs = allColumns.reduce((acc, column, index) => {
      acc.push({ column, value: validValues[index] });
      return acc;
    }, []);

    // 构建 SQL 语句
    const sql = `INSERT INTO messages (${columnValuePairs
      .map((pair) => pair.column)
      .join(", ")}) VALUES (${columnValuePairs
      .map((pair) => (pair.value === "DEFAULT" ? "DEFAULT" : `"${pair.value}"`))
      .join(", ")})`;

    return await sqlCore.allSqlAction(sql);
  }
}

export default new chatModel();
